package com.rustic.server.dto.user;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.xy.commons.exception.JzRuntimeException;
import cn.xy.commons.validator.EnumValidator;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.rustic.server.enums.CardType;
import com.rustic.server.enums.GenderEnum;
import com.rustic.server.po.User;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.hibernate.validator.constraints.Length;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotEmpty;
import java.util.Date;

/**
 * 表[user]对应UpdateDTO类
 *
 * @author auto
 */

@Data
public class UserUpdateDTO {

    @Min(value = 1, message = "用户ID不能为空")
    @ApiModelProperty(value = "用户ID", dataType = "int")
    private Integer id;

    @ApiModelProperty(value = "手机号码(必填)", dataType = "String", required = true)
    private String mobile;

    @ApiModelProperty(value = "用户邮箱", dataType = "String")
    private String email;

    @Length(min = 0, max = 500, message = "头像路径长度为0-500个字符")
    @ApiModelProperty(value = "头像", dataType = "String")
    private String avatar;

    @ApiModelProperty(value = "手机区号", dataType = "String", required = true)
    private String mobilePrefix;

    @Length(min = 0, max = 64, message = "会员姓名长度为0-64个字符")
    @ApiModelProperty(value = "会员姓名", dataType = "String")
    private String name;

    @Length(min = 0, max = 64, message = "名长度为0-64个字符")
    @ApiModelProperty(value = "名", dataType = "String")
    private String firstName;

    @Length(min = 0, max = 64, message = "姓长度为0-64个字符")
    @ApiModelProperty(value = "姓", dataType = "String")
    private String lastName;

    @EnumValidator(clazz = CardType.class, message = "请填写正确的证件类型")
    @ApiModelProperty(value = "证件类型(1身份证 2港澳台居民居住证 3护照 4其他)", dataType = "int", required = true)
    private int cardType;

    @NotEmpty(message = "证件号不能为空")
    @Length(min = 0, max = 128, message = "证件号长度为0-128个字符")
    @ApiModelProperty(value = "证件号", dataType = "String", required = true)
    private String idCard;

    @EnumValidator(clazz = GenderEnum.class, message = "请填写正确的性别")
    @ApiModelProperty(value = "性别(1男 2女 3未知)", dataType = "int", required = true)
    private int gender;

    @NotEmpty(message = "国籍不能为空")
    @Length(min = 0, max = 255, message = "证件号长度为0-255个字符")
    @ApiModelProperty(value = "国籍", dataType = "String", required = true)
    private String nationality;

    @Length(min = 0, max = 512, message = "通信地址长度为0-512个字符")
    @ApiModelProperty(value = "通讯地址", dataType = "String")
    private String address;

    @Length(min = 0, max = 255, message = "会员权益长度为0-255个字符")
    @ApiModelProperty(value = "会员权益(1贵宾区 2发起聊天 3隐私设置), 多个时逗号分隔", dataType = "String")
    private String rights;

    @NotEmpty(message = "角色不能为空")
    @Length(min = 0, max = 1024, message = "角色长度为0-1024个字符")
    @ApiModelProperty(value = "角色id,1普通会员,2嘉宾,3赞助商(逗号分隔)", dataType = "String", required = true)
    private String roleIds;

    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    @ApiModelProperty(value = "注册认证时间(yyyy-MM-dd)", dataType = "Date", required = true)
    private Date registerTime;

    public User toUser() {
        User user = new User();
        BeanUtil.copyProperties(this, user);
        return user;
    }

    public void valid() {
        if (this.getName().isEmpty() && (this.getLastName().isEmpty() || this.getFirstName().isEmpty())) {
            throw new JzRuntimeException("中文姓名”、“姓（Last Name）&名（First Name）,二选一必填");
        }
        if (CharSequenceUtil.isEmpty(this.getMobile()) && CharSequenceUtil.isEmpty(this.getEmail())) {
            throw new JzRuntimeException("手机号与邮箱必须填写其中一项");
        }
    }
}
